package com.example.ximalayademo.presenters;

import com.example.ximalayademo.api.XimalayaApi;
import com.example.ximalayademo.interfaces.ISearchCallback;
import com.example.ximalayademo.interfaces.ISearchPresenter;
import com.example.ximalayademo.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.album.GussLikeAlbumList;
import com.ximalaya.ting.android.opensdk.model.album.SearchAlbumList;
import com.ximalaya.ting.android.opensdk.model.word.HotWord;
import com.ximalaya.ting.android.opensdk.model.word.HotWordList;
import com.ximalaya.ting.android.opensdk.model.word.QueryResult;
import com.ximalaya.ting.android.opensdk.model.word.SuggestWords;

import java.util.ArrayList;
import java.util.List;

public class SearchPresenter implements ISearchPresenter {

    //搜索当前关键词
    private String mCurrentKeyword = null;
    private final XimalayaApi mXimalayaApi;
    private static final int DEFAULT_PAGE = 1;
    private int mCurrentPage = 1;
    private String TAG = "SearchPresenter";

    private SearchPresenter(){
        mXimalayaApi = XimalayaApi.getXimalayaApi();
    }

    private volatile static  SearchPresenter sSearchPresenter = null;

    public static SearchPresenter getSearchPresenter(){
        if (sSearchPresenter == null) {
            synchronized (SearchPresenter.class){
                if (sSearchPresenter == null) {
                    sSearchPresenter = new SearchPresenter();
                }
            }
        }
        return sSearchPresenter;
    }

    private List<ISearchCallback> mCallbacks = new ArrayList<>();

    @Override
    public void doSearch(String keyword) {
        //用于新搜索
        //当网络不好的时候，用户会点击重新搜索
        this.mCurrentKeyword = keyword;
        search(keyword);
    }

    private void search(String keyword) {
        mXimalayaApi.searchByKeyword(keyword, mCurrentPage, new IDataCallBack<SearchAlbumList>() {
            @Override
            public void onSuccess(SearchAlbumList searchAlbumList) {
                List<Album> albums = searchAlbumList.getAlbums();
                if (albums != null) {
                    LogUtil.d(TAG, "albums size --- > " + albums.size());
                    for (ISearchCallback iSearchCallback : mCallbacks) {
                        iSearchCallback.onSearchResultLoaded(albums);
                    }
                } else {
                    LogUtil.d(TAG, "albums is null..");
                }
            }

            @Override
            public void onError(int errorCode, String errorMsg) {
                LogUtil.d(TAG, "errorCode --- > " + errorCode);
                LogUtil.d(TAG, "errorMsg --- > " + errorMsg);
                for (ISearchCallback iSearchCallback : mCallbacks) {
                    iSearchCallback.onError(errorCode, errorMsg);
                }
            }
        });
    }

    @Override
    public void reSearch() {
        search(mCurrentKeyword);
    }

    @Override
    public void loadMore() {

    }

    @Override
    public void getHotWord() {
        mXimalayaApi.getHotWords(new IDataCallBack<HotWordList>() {
            @Override
            public void onSuccess(HotWordList hotWordList) {
                if (hotWordList != null) {
                    List<HotWord> hotWords = hotWordList.getHotWordList();
                    LogUtil.d(TAG ,"hotWords size --- > "+hotWords.size()) ;
                    for (ISearchCallback iSearchCallBack : mCallbacks) {
                        iSearchCallBack.onHotWordLoaded(hotWords);
                    }
                }
            }

            @Override
            public void onError(int errorCode, String errorMsg) {
                LogUtil.d(TAG, "getHotWord  errorCode --> " + errorCode);
                LogUtil.d(TAG, "getHotWord  errorMsg --> " + errorMsg);
                for (ISearchCallback iSearchCallBack : mCallbacks) {
                    iSearchCallBack.onError(errorCode , errorMsg);
                }
            }
        });
    }

    @Override
    public void getRecommendWord(String keyword) {
        mXimalayaApi.getSuggestWord(keyword, new IDataCallBack<SuggestWords>() {
            @Override
            public void onSuccess(SuggestWords suggestWords) {
                if (suggestWords != null) {
                    List<QueryResult> keyWordList = suggestWords.getKeyWordList();
                    LogUtil.d(TAG, "keyWordList size --- > " + keyWordList.size());
                    for (ISearchCallback iSearchCallback : mCallbacks) {
                        iSearchCallback.onRecommendWordLoaded(keyWordList);
                    }
                }
            }

            @Override
            public void onError(int errorCode, String errorMsg) {
                LogUtil.d(TAG, "getRecommendWord errorCode --- > " + errorCode);
                LogUtil.d(TAG, "getRecommendWord errorMsg --- > " + errorMsg);
            }
        });
    }


    @Override
    public void registerViewCallBack(ISearchCallback iSearchCallBack) {
        if (!mCallbacks.contains(iSearchCallBack)) {
            mCallbacks.add(iSearchCallBack);
        }
    }

    @Override
    public void unRegisterViewCallBack(ISearchCallback iSearchCallBack) {
        mCallbacks.remove(iSearchCallBack);

    }
}
